package com.smartcar.client.util;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.smartcar.client.domin.LineRequest;
import lombok.extern.log4j.Log4j2;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 高德路线工具类
 * @author : 高文娜大美女
 * @className : PathUtil
 * @description : 描述说明该类的功能
 * @createTime : 2023/10/19 14:40
 */
@Log4j2
public class PathUtil {
    private static final String KEY = "b4b97cbbc0b6cb1979fe19453a34a1f5";
    public static final String PATH = "PATH";
    public static final String ADDRESS = "ADDRESS";


    public static void show() {
        String body = HttpUtil.createGet("https://tsapi.amap.com/v1/track/terminal/trsearch?key=b4b97cbbc0b6cb1979fe19453a34a1f5&sid=1&trid=1&tid=1&starttime=1532432593000&endtime=1532518993000&correction=denoise=1,mapmatch=0,attribute=0,threshold=0,mode=driving&page=1&pagesize=20").
                execute().charset("utf-8").body();
        log.info(body);
    }

    /**
     * 驾车路线规划
     *预估价格
     * @param
     */

    public static List<Map<String,Object>> queryLine(LineRequest lineRequest){
        String body = HttpUtil.createGet("https://restapi.amap.com/v5/direction/driving?key=b4b97cbbc0b6cb1979fe19453a34a1f5&output=JSON&origin="+lineRequest.getEndX()+","+lineRequest.getEndY()+"&destination="+lineRequest.getStartX()+","+lineRequest.getStartY()+"&show_fields=polyline")
                .execute()
                .charset("utf-8").body();
        log.info(body);
        Map map = JSON.parseObject(body, Map.class);
        log.info(JSON.toJSONString(map));
        Map o =(Map) map.get("route");
        String taxiCost = (String)o.get("taxi_cost");
        log.info("预估价格："+taxiCost);
        JSONArray paths = JSON.parseArray(JSON.toJSONString(o.get("paths")));
        List<Map> javaList = paths.toJavaList(Map.class);
        List<Map<String, Object>> lists = new ArrayList<>();
        AtomicInteger atomicInteger1 = new AtomicInteger(0);
        javaList.stream().forEach(map1 -> {
            List<BigDecimal[]> strings = new ArrayList<>();
            JSONArray steps = JSON.parseArray(JSON.toJSONString(map1.get("steps")));
            List<Map> javaList2 = steps.toJavaList(Map.class);
            javaList2.stream().forEach(map2 ->{
                String s = String.valueOf(map2.get("polyline"));
                String[] split = s.split(";");
                Arrays.stream(split).forEach(s1 -> {
                    String[] split1 = s1.split(",");
                    AtomicInteger atomicInteger = new AtomicInteger(0);
                    BigDecimal[] strings1 = new BigDecimal[split1.length];
                    Arrays.stream(split1).forEach(s2 -> {
                        strings1[atomicInteger.getAndIncrement()] = new BigDecimal(s2);
                        strings.add(strings1);
                    });
                });
            });
            Map<String, Object> hash = new HashMap<>();
            hash.put(PATH,"线路"+atomicInteger1.getAndIncrement());
            hash.put(ADDRESS,strings);
            lists.add(hash);
        });
        HashMap<String, Object> map1 = new HashMap<>();
        map1.put("taxiCost",taxiCost);
        lists.add(map1);
        return lists;
    }
    public void show2(String origin, String destination) {
        String body = HttpUtil.createGet("https://restapi.amap.com/v5/direction/driving?key=b4b97cbbc0b6cb1979fe19453a34a1f5&output=JSON&origin=" + origin + "&destination=" + destination + "")
                .execute()
                .charset("utf-8").body();
        log.info(body);
    }

    public static void main(String[] args) {

//        ArrayList<Map<String, Object>> list = new ArrayList<>();
//        for (int i = 0; i < 4; i++) {
//            Map<String, Object> map = new HashMap<>();
//            map.put("name","Test_"+i);
//            map.put("age",i);
//        }
//        List<Map<String, Object>> collect = list.stream().sorted((map1, map2) -> {
//            int age = (int) map1.getOrDefault("age", 0);
//            int age1 = (int) map2.getOrDefault("age", 0);
//            return age - age1;
//        }).collect(Collectors.toList());

        //TODO 要求7位数字,要求10个

        List<Long> longList = new Random().ints(10, 1000000, 9999999)
                .asLongStream()
                .boxed().collect(Collectors.toList());
        log.info(longList);
    }

}

